//
//  TrainViewController.m
//  dogGroup
//
//  Created by iOS Two on 14-8-13.
//  Copyright (c) 2014年 iOS Two. All rights reserved.
//

#import "TrainViewController.h"
#import "TrainDetailViewController.h"

@interface TrainViewController ()

@end

@implementation TrainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated {
    [MobClick beginLogPageView:@"TrainViewController"];
}
-(void)viewDidDisappear:(BOOL)animated {
    [MobClick endLogPageView:@"TrainViewController"];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self creatController];
    if (![self netWorkCheck]) {
        [self alertViewShow:@"网络未连接"];
    } else {
        [self getData];
    }
    if (version >= 7) {
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.95 green:0.76 blue:0.17 alpha:1];
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
        titleLabel.text = @"狗狗训练";
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont systemFontOfSize:20];
        titleLabel.textColor = [UIColor whiteColor];
        self.navigationItem.titleView = titleLabel;
    } else {
        self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.95 green:0.76 blue:0.17 alpha:1];
        self.navigationItem.title = @"狗狗训练";
    }
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]init];
    self.navigationItem.backBarButtonItem = backItem;
    backItem.title = @"返回";
}

-(void)getData {
    [self showIndeterminateProgress];
    _trainArray = [[NSMutableArray alloc]init];
    _trainNameArray = [[NSMutableArray alloc]init];
#warning 改dogId
    AVQuery *query = [[AVQuery alloc]initWithClassName:@"dogTrain"];
    [query whereKey:@"dogId" equalTo:@"53eacc0ce4b0540d07bda86b"];
    [query orderByAscending:@"createdAt"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        for (int i = 0; i<objects.count; i++) {
            AVObject *object = [objects objectAtIndex:i];
            [_trainArray addObject:[object objectForKey:@"trainContent"]];
            [_trainNameArray addObject:[object objectForKey:@"trainTitle"]];
        }
        [_tableView reloadData];
        [HUD hide:YES];
    }];
}

-(void)creatController {
    _tableView = [[UITableView alloc]init];
    if (version >= 7) {
        _tableView.frame = CGRectMake(0, 0, 320, kHeight - 64);
    } else {
        _tableView.frame = CGRectMake(0, 0, 320, kHeight - 44);
    }
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 54;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.scrollEnabled = NO;
    UIImageView *background = [[UIImageView alloc]initWithFrame:self.view.frame];
    background.image = [UIImage imageNamed:@"img_mainbackground"];
    [_tableView setBackgroundView:background];
    [self.view addSubview:_tableView];
    [self setExtraCellLineHidden:_tableView];
}

//隐藏表格多余分割线
- (void)setExtraCellLineHidden: (UITableView *)tableView {
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    [tableView setTableHeaderView:view];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _trainNameArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    } else {
        for (UIView *view in [cell.contentView subviews]) {
            [view removeFromSuperview];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row < _trainNameArray.count) {
        UIImageView *back = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 54)];
        back.image = [UIImage imageNamed:@"img_cellback3"];
        [cell.contentView addSubview:back];
        
        UIImageView *image =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 54)];
        image.image = [UIImage imageNamed:@"img_cellback2"];
        [cell.contentView addSubview:image];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 11, 266, 34)];
        label.text = [_trainNameArray objectAtIndex:indexPath.row];
        label.textAlignment = NSTextAlignmentLeft;
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor colorWithRed:0.06 green:0.42 blue:0.68 alpha:1];
        label.font = [UIFont systemFontOfSize:16];
        [cell.contentView addSubview:label];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [MobClick event:@"TrainVCDogTrain"];

    TrainDetailViewController *detail = [[TrainDetailViewController alloc]init];
    detail.trainTitle = [_trainNameArray objectAtIndex:indexPath.row];
    detail.trainInfo = [_trainArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detail animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
